<?php
namespace app\admin\controller;

use app\admin\logic\UserLogic;
use think\captcha\Captcha;
use think\Controller;

class Index extends Controller {
	// 登陆页面
	public function index(){

		return $this->fetch();
	}
	public function captcha(){
		$captcha = new Captcha();
		return $captcha->entry('');
	}

	/**
	 * 登陆操作
	 * @param string $param 前台传过来的json数据
	 * @return string
	 */
	public function login($param=''){
		$userArr=json_decode($param,true);
		// if(!captcha_check($userArr['verify'])){
		// 	return ['code'=>-1];
		// }else{
		$userLogic=new UserLogic();
		$user=$userLogic->login($userArr['lname'],$userArr['pwd']);
		if($user){
			session('name',$user['lname']);
			session('id',$user['id']);
			session('role',$user['roles']);
            session('branch_id',$user['branch_id']);
			$redirect_url='';
			switch($user['roles']){
				case '0':
                case '1':
					$redirect_url=url('main/main');
					break;
				case '4':
					$redirect_url=url('main/moduleFront');
					break;
				default:
					//$redirect_url=url('main/moduleTech');
                    $redirect_url=url('/');
					break;
			}
			return ['code'=>1,'url'=>$redirect_url];
		}else{
			return ['code'=>0];
		}
		// }
		// return ['code'=>0];
	}


	/**
	 * 退出登陆
 	 */
	public function logout(){
		session('name',null);
		session('id',null);
		$this->redirect("/admin/index/index");
	}

	/**
	 * 404页面
	 * @return mixed
	 */
	public function p404(){
		return $this->fetch('404');
	}
}
